namespace com.stifel.Global
{
    using System;
    using System.Runtime.InteropServices;

    using com.stifel.SharePoint;

    using Microsoft.SharePoint;

    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid( "be8ff5a0-71e0-4578-bb27-3f949aa511ca" )]
    public class GlobalEventReceiver : SPFeatureReceiver
    {
        #region Fields

        public const string FEATURE = "GlobalLists";

        #endregion Fields

        #region Methods

        public override void FeatureActivated( SPFeatureReceiverProperties properties )
        {
            try
            {
                if ( !( properties.Feature.Parent is SPSite ) )
                {
                    throw new Exception( string.Format( "{0} :: Error activating feature. Feature parent is not of type SPSite.", FEATURE ) );
                }

                CustomLoggingService.LogMessage( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: Feature activating.", FEATURE );

                using ( Timezone_Definition o = new Timezone_Definition( properties ) )
                    o.Create( );

                using ( USStates_Definition o = new USStates_Definition( properties ) )
                    o.Create( );

                CustomLoggingService.LogMessage( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: Feature activated.", FEATURE );
            }
            catch (Exception e)
            {
                throw new Exception( string.Format( "Error occurred in {0} FeatureActivated. {1} {2}", FEATURE, e.Message, e.StackTrace ), e );
            }
        }

        public override void FeatureDeactivating( SPFeatureReceiverProperties properties )
        {
            try
            {
                if ( !( properties.Feature.Parent is SPSite ) )
                {
                    throw new Exception( string.Format( "{0} :: Error activating feature. Feature parent is not of type SPSite.", FEATURE ) );
                }

                CustomLoggingService.LogMessage( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: Feature deactivating.", FEATURE );

                using ( Timezone_Definition o = new Timezone_Definition( properties ) )
                    o.Delete( );

                using ( USStates_Definition o = new USStates_Definition( properties ) )
                    o.Delete( );

                CustomLoggingService.LogMessage( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: Feature deactivated.", FEATURE );
            }
            catch (Exception e)
            {
                throw new Exception( string.Format( "Error occurred in {0} FeatureDeactivating. {1} {2}", FEATURE, e.Message, e.StackTrace ), e );
            }
        }

        #endregion Methods
    }
}